/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.City;
import org.springblade.modules.system.service.ICityService;
import org.springblade.modules.system.vo.CityVO;
import org.springblade.modules.system.wrapper.CityWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 行政区划 控制器
 *
 * @author Blade
 * @since 2022-07-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-system/city")
@Api(value = "行政区划", tags = "行政区划接口")
public class CityController extends BladeController {

	private ICityService cityService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入city")
	public R<CityVO> detail(City city) {
		City detail = cityService.getOne(Condition.getQueryWrapper(city));
		return R.data(CityWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 行政区划
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入city")
	public R<IPage<City>> list(City city, Query query) {
		IPage<City> pages = cityService.page(Condition.getPage(query), Condition.getQueryWrapper(city));
		return R.data(pages);
	}

	/**
	 * 懒加载列表
	 */
	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "懒加载列表", notes = "传入city")
	public R<List<INode>> lazyList(Long parentId, @ApiIgnore @RequestParam Map<String, Object> param) {
		List<INode> list = cityService.lazyList(parentId, param);
		return R.data(CityWrapper.build().listNodeLazyVO(list));
	}

	/**
	 * 懒加载列表
	 */
	@GetMapping("/lazy-tree")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "区划编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "区划名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "懒加载列表", notes = "传入city")
	public R<List<INode>> lazyTree(Long parentId, @ApiIgnore @RequestParam Map<String, Object> param) {
		List<INode> list = cityService.lazyTree(parentId, param);
		return R.data(CityWrapper.build().listNodeLazyVO(list));
	}

	/**
	 * 获取区划树形结构
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<INode>> recursionTree() {
		List<INode> tree = cityService.recursionTree();
		return R.data(tree);
	}

	/**
	 * 新增 行政区划
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入city")
	public R save(@Valid @RequestBody City city) {
		return R.status(cityService.save(city));
	}

	/**
	 * 修改 行政区划
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入city")
	public R update(@Valid @RequestBody City city) {
		return R.status(cityService.updateById(city));
	}

	/**
	 * 新增或修改 行政区划
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入city")
	public R submit(@Valid @RequestBody City city) {
		return R.status(cityService.saveOrUpdate(city));
	}


	/**
	 * 删除 行政区划
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(cityService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 行政区划下拉数据源
	 */
	@GetMapping("/select")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉数据源", notes = "传入 id")
	public R<List<City>> select(@RequestParam(required = false, defaultValue = "0") Long id) {
		List<City> list = cityService.list(Wrappers.<City>query().lambda().eq(City::getParentId, id));
		return R.data(list);
	}

	/**
	 * 行政区划下拉数据源
	 */
	@GetMapping("/all")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "获取全部数据")
	public R<List<City>> all() {
		List<City> list = cityService.list();
		return R.data(list);
	}
}
